package com.example.day02.parnterfrag;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.day02.Custom.DLRoundImageView;
import com.example.day02.R;
import com.example.day02.utils.AssetsUtils;
import com.example.day02.utils.HttpUtils;
import com.example.day02.utils.LoadNetDataAsyncTask;
import com.example.day02.utils.URLUtils;
import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

public class MatchActivity extends AppCompatActivity implements View.OnClickListener , LoadNetDataAsyncTask.onGetNetDataListener{
    private static final String TAG = "MatchActivity";
    DLRoundImageView manIv,womanIv;
     ImageView backIv;
     TextView manTv,womanTv,pdTv,vsTv,pfTv,bzTv,jxTv,zyTv,titleTv;
     String boy_name,boy_logo,girl_name,girl_logo;
    private String parnterURL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match);
        initView();
        //接收值的方法
        getLastData();
        String manName=boy_name.replace("座","");
        Log.d(TAG, "manName: "+manName);

        String womanName=girl_name.replace("座","");
        Log.d(TAG, "womanName: "+womanName);
        try {
            String boy = URLEncoder.encode(manName, "UTF-8");
            String girl = URLEncoder.encode(womanName, "UTF-8");
            Log.d(TAG, "boy: "+boy);
            Log.d(TAG, "girl: "+girl);
            parnterURL = URLUtils.getParnterURL(boy, girl);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        new LoadNetDataAsyncTask(this,this).execute(parnterURL);
        Map<String, Bitmap> contentLogoImgMap = AssetsUtils.getContentLogoImgMap();
        Bitmap boyBm = contentLogoImgMap.get(boy_logo);
        Log.d(TAG, "boyBm: "+boyBm);
        manIv.setImageBitmap(boyBm);
        Bitmap girlBm = contentLogoImgMap.get(girl_logo);
        womanIv.setImageBitmap(girlBm);
        //开始进行网络请求

    }

    /*
    接收的传值
     */
    private void getLastData() {
        Intent intent = getIntent();
       boy_name= intent.getStringExtra("boy_name");
       boy_logo=intent.getStringExtra("boy_logo");
       girl_name= intent.getStringExtra("girl_name");
       girl_logo=intent.getStringExtra("girl_logo");
        //设置能显示的控件


//        manIv.setImageResource(R.mipmap.icon_baiyang);
//        //womanIv.setImageBitmap(GirlIv);
        manTv.setText(boy_name);
        womanTv.setText(girl_name);
        pdTv.setText("星座配对~"+boy_name+"和"+girl_name+"配对~");
        vsTv.setText(boy_name+"  vs  "+girl_name);
    }

    //初始化控件
    private void initView() {
        manIv=findViewById(R.id.partanalysis_iv_man);
        manIv.setImageResource(R.drawable.baiyang);
        womanIv=findViewById(R.id.partanalysis_iv_woman);
        womanIv.setImageResource(R.drawable.baiyang);
        manTv=findViewById(R.id.partanalysis_tv_man);
        womanTv=findViewById(R.id.partanalysis_tv_woman);
        pdTv=findViewById(R.id.partanalysis_tv_pd);
        vsTv=findViewById(R.id.partanalysis_tv_vs);
        pfTv=findViewById(R.id.partanalysis_tv_pf);
        bzTv=findViewById(R.id.partanalysis_tv_bz);
        jxTv=findViewById(R.id.partanalysis_tv_jx);
        zyTv=findViewById(R.id.partanalysis_tv_zy);
        backIv=findViewById(R.id.title_iv_back);
        titleTv=findViewById(R.id.title_tv);

        titleTv.setText("配对分析");
        backIv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_iv_back:
                finish();
                break;
        }
    }

    private void ShowData(String json) {
        //解析数据
        Gson gson = new Gson();
        ParnterBean parnterBean = gson.fromJson(json, ParnterBean.class);
        ParnterBean.ResultBean resultBean = parnterBean.getResult();
        pfTv.setText("配对评分: " + resultBean.getZhishu() + "  " + resultBean.getJieguo());
        bzTv.setText("星座比重: " + resultBean.getBizhong());
        jxTv.setText("解析:\n\n" + resultBean.getLianai());
        zyTv.setText("注意事项:\n\n" + resultBean.getZhuyi());
    }
    @Override
    public void showDataFromNet(String s) {
      ShowData(s);
    }
}
